﻿
Public Class Anagrafica

    Public Property PK_Anagrafica As Integer

    Public Property Denominazione As String

    Public Property Referente As String

    Public Property Telefono As String

    Public Property Fax As String

    Public Property Mail As String

    Public Property Deleted As Boolean

    Public ReadOnly Property DefinizioneCompleta
        Get
            Dim strRet As String = ""
            If Not String.IsNullOrEmpty(Denominazione) Then strRet &= Denominazione
            If Not String.IsNullOrEmpty(Telefono) Then strRet &= " tel. " & Telefono
            If Not String.IsNullOrEmpty(Referente) Then strRet &= " chiedere di " & Referente & vbCrLf
            If Not String.IsNullOrEmpty(Fax) Then strRet &= " Fax. " & Fax
            If Not String.IsNullOrEmpty(Mail) Then strRet &= " Mail " & Mail
            Return strRet
        End Get
    End Property

    Public Sub LoadFromPk(objpk As Integer)

        Dim sql As String = "SELECT * FROM Anagrafica WHERE PK_Anagrafica = " & objpk
        Dim ds As New DataSet
        Dim dbO As New DBObject
        ds = dbO.FillDS(sql, Nothing, 1)
        If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            PK_Anagrafica = objpk
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Denominazione")) Then
                Denominazione = ds.Tables(0).Rows(0).Item("Denominazione").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Referente")) Then
                Referente = ds.Tables(0).Rows(0).Item("Referente").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Telefono")) Then
                Telefono = ds.Tables(0).Rows(0).Item("Telefono")
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Fax")) Then
                Fax = ds.Tables(0).Rows(0).Item("Fax")
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Mail").ToString) Then
                Mail = ds.Tables(0).Rows(0).Item("Mail").ToString
            End If

        End If

    End Sub

    Public Function Salva() As Integer
        If PK_Anagrafica = 0 Then
            Dim dbo As New DBObject
            Dim sql As String = "INSERT INTO Anagrafica (Denominazione, Referente, Telefono, Fax, Mail, Deleted) VALUES( " & _
                                " '" & Denominazione.Replace("'", "''") & "'," & _
                                " '" & Referente.Replace("'", "''") & "'," & _
                                " '" & Telefono & "'," & _
                                " '" & Fax & "'," & _
                                " '" & Mail & "', 0)"

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then
                sql = "SELECT MAX (PK_Anagrafica) FROM Anagrafica"
                dbo.ExecuteScalar(sql, Nothing, CommandType.Text)
                Return dbo.ExecuteScalar(sql, Nothing, CommandType.Text)
            Else
                Return 0
            End If
        Else
            Dim dbo As New DBObject
            Dim sql As String = "UPDATE Anagrafica SET " & _
                                " Denominazione= '" & Denominazione.Replace("'", "''") & "'" & _
                                " ,Referente= '" & Referente.Replace("'", "''") & "'" & _
                                " ,Telefono= '" & Telefono & "'" & _
                                " ,Fax= '" & Fax & "'" & _
                                " ,Mail= '" & Mail & "'" & _
                                " WHERE PK_Anagrafica = " & PK_Anagrafica

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                Return PK_Anagrafica
            Else
                Return 0
            End If

        End If
    End Function

End Class
